package com.google.android.gms.common.internal;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.BinderThread;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Feature;
import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.android.gms.common.util.VisibleForTesting;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.concurrent.GuardedBy;
import o.aoj;
import o.aot;
import o.aov;
import o.apa;
import o.apf;
import o.aqo;

@KeepForSdk
public abstract class BaseGmsClient<T extends IInterface> {
    private static final Feature[] ˊ = new Feature[0];
    @KeepForSdk
    public static final String[] ˋ = new String[]{"service_esmobile", "service_googleme"};
    private int ʻ;
    @GuardedBy("mLock")
    private int ʻॱ;
    private long ʼ;
    private long ʽ;
    private final String ʽॱ;
    private final c ʾ;
    private boolean ʿ;
    private ConnectionResult ˈ;
    private final aov ˊॱ;
    private final Object ˋॱ;
    final Handler ˎ;
    @VisibleForTesting
    protected b ˏ;
    private final Context ˏॱ;
    @VisibleForTesting
    private apf ͺ;
    @VisibleForTesting
    protected AtomicInteger ॱ;
    private final Object ॱˊ;
    @GuardedBy("mServiceBrokerLock")
    private aot ॱˋ;
    @GuardedBy("mLock")
    private e ॱˎ;
    private int ॱॱ;
    @GuardedBy("mLock")
    private T ॱᐝ;
    private long ᐝ;
    private final ArrayList<a<?>> ᐝॱ;

    protected abstract class a<TListener> {
        private boolean ˋ = false;
        private TListener ˎ;
        private final /* synthetic */ BaseGmsClient ˏ;

        public a(BaseGmsClient baseGmsClient, TListener tListener) {
            this.ˏ = baseGmsClient;
            this.ˎ = tListener;
        }

        protected abstract void ˋ(TListener tListener);

        protected abstract void ˏ();

        public final void ˋ() {
            synchronized (this) {
                Object obj = this.ˎ;
                if (this.ˋ) {
                    String valueOf = String.valueOf(this);
                    Log.w("GmsClient", new StringBuilder(String.valueOf(valueOf).length() + 47).append("Callback proxy ").append(valueOf).append(" being reused. This is not safe.").toString());
                }
            }
            if (obj != null) {
                try {
                    ˋ(obj);
                } catch (RuntimeException e) {
                    ˏ();
                    throw e;
                }
            }
            ˏ();
            synchronized (this) {
                this.ˋ = true;
            }
            ॱ();
        }

        public final void ॱ() {
            ˊ();
            synchronized (this.ˏ.ᐝॱ) {
                this.ˏ.ᐝॱ.remove(this);
            }
        }

        public final void ˊ() {
            synchronized (this) {
                this.ˎ = null;
            }
        }
    }

    @KeepForSdk
    public interface b {
        @KeepForSdk
        void ˊ(@NonNull ConnectionResult connectionResult);
    }

    @KeepForSdk
    public interface c {
        @KeepForSdk
        void ˊ(int i);
    }

    abstract class d extends a<Boolean> {
        private final Bundle ˊ;
        private final int ˋ;
        private final /* synthetic */ BaseGmsClient ˏ;

        @BinderThread
        protected d(BaseGmsClient baseGmsClient, int i, Bundle bundle) {
            this.ˏ = baseGmsClient;
            super(baseGmsClient, Boolean.valueOf(true));
            this.ˋ = i;
            this.ˊ = bundle;
        }

        protected abstract void ˊ(ConnectionResult connectionResult);

        protected abstract boolean ˎ();

        protected final void ˏ() {
        }

        protected final /* synthetic */ void ˋ(Object obj) {
            PendingIntent pendingIntent = null;
            if (((Boolean) obj) == null) {
                this.ˏ.ॱ(1, null);
                return;
            }
            switch (this.ˋ) {
                case 0:
                    if (!ˎ()) {
                        this.ˏ.ॱ(1, null);
                        ˊ(new ConnectionResult(8, null));
                        return;
                    }
                    return;
                case 10:
                    this.ˏ.ॱ(1, null);
                    throw new IllegalStateException(String.format("A fatal developer error has occurred. Class name: %s. Start service action: %s. Service Descriptor: %s. ", new Object[]{getClass().getSimpleName(), this.ˏ.ॱ(), this.ˏ.ˋ()}));
                default:
                    this.ˏ.ॱ(1, null);
                    if (this.ˊ != null) {
                        pendingIntent = (PendingIntent) this.ˊ.getParcelable("pendingIntent");
                    }
                    ˊ(new ConnectionResult(this.ˋ, pendingIntent));
                    return;
            }
        }
    }

    @VisibleForTesting
    public final class e implements ServiceConnection {
        private final /* synthetic */ BaseGmsClient ˋ;
        private final int ॱ;

        public e(BaseGmsClient baseGmsClient, int i) {
            this.ˋ = baseGmsClient;
            this.ॱ = i;
        }

        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            if (iBinder == null) {
                this.ˋ.ˏ(16);
                return;
            }
            synchronized (this.ˋ.ˋॱ) {
                aot o_aot;
                BaseGmsClient baseGmsClient = this.ˋ;
                if (iBinder == null) {
                    o_aot = null;
                } else {
                    IInterface queryLocalInterface = iBinder.queryLocalInterface("com.google.android.gms.common.internal.IGmsServiceBroker");
                    if (queryLocalInterface == null || !(queryLocalInterface instanceof aot)) {
                        o_aot = new o.aot.b.b(iBinder);
                    } else {
                        o_aot = (aot) queryLocalInterface;
                    }
                }
                baseGmsClient.ॱˋ = o_aot;
            }
            this.ˋ.ˎ(0, null, this.ॱ);
        }

        public final void onServiceDisconnected(ComponentName componentName) {
            synchronized (this.ˋ.ˋॱ) {
                this.ˋ.ॱˋ = null;
            }
            this.ˋ.ˎ.sendMessage(this.ˋ.ˎ.obtainMessage(6, this.ॱ, 1));
        }
    }

    protected final class f extends d {
        private final /* synthetic */ BaseGmsClient ˋ;

        @BinderThread
        public f(BaseGmsClient baseGmsClient, int i, @Nullable Bundle bundle) {
            this.ˋ = baseGmsClient;
            super(baseGmsClient, i, null);
        }

        protected final void ˊ(ConnectionResult connectionResult) {
            if (this.ˋ.ʼ() && this.ˋ.ॱˊ()) {
                this.ˋ.ˏ(16);
                return;
            }
            this.ˋ.ˏ.ˊ(connectionResult);
            this.ˋ.ˋ(connectionResult);
        }

        protected final boolean ˎ() {
            this.ˋ.ˏ.ˊ(ConnectionResult.ˎ);
            return true;
        }
    }

    final class zzc extends aqo {
        private final /* synthetic */ BaseGmsClient ॱ;

        public final void handleMessage(Message message) {
            PendingIntent pendingIntent = null;
            if (this.ॱ.ॱ.get() != message.arg1) {
                if (ˎ(message)) {
                    ॱ(message);
                }
            } else if ((message.what == 1 || message.what == 7 || ((message.what == 4 && !this.ॱ.ʼ()) || message.what == 5)) && !this.ॱ.ʽ()) {
                ॱ(message);
            } else if (message.what == 4) {
                this.ॱ.ˈ = new ConnectionResult(message.arg2);
                if (!this.ॱ.ॱˊ() || this.ॱ.ʿ) {
                    if (this.ॱ.ˈ != null) {
                        r0 = this.ॱ.ˈ;
                    } else {
                        r0 = new ConnectionResult(8);
                    }
                    this.ॱ.ˏ.ˊ(r0);
                    this.ॱ.ˋ(r0);
                    return;
                }
                this.ॱ.ॱ(3, null);
            } else if (message.what == 5) {
                if (this.ॱ.ˈ != null) {
                    r0 = this.ॱ.ˈ;
                } else {
                    r0 = new ConnectionResult(8);
                }
                this.ॱ.ˏ.ˊ(r0);
                this.ॱ.ˋ(r0);
            } else if (message.what == 3) {
                if (message.obj instanceof PendingIntent) {
                    pendingIntent = (PendingIntent) message.obj;
                }
                ConnectionResult connectionResult = new ConnectionResult(message.arg2, pendingIntent);
                this.ॱ.ˏ.ˊ(connectionResult);
                this.ॱ.ˋ(connectionResult);
            } else if (message.what == 6) {
                this.ॱ.ॱ(5, null);
                if (this.ॱ.ʾ != null) {
                    this.ॱ.ʾ.ˊ(message.arg2);
                }
                this.ॱ.ˋ(message.arg2);
                this.ॱ.ˊ(5, 1, null);
            } else if (message.what == 2 && !this.ॱ.ˎ()) {
                ॱ(message);
            } else if (ˎ(message)) {
                ((a) message.obj).ˋ();
            } else {
                Log.wtf("GmsClient", "Don't know how to handle message: " + message.what, new Exception());
            }
        }

        private static void ॱ(Message message) {
            a aVar = (a) message.obj;
            aVar.ˏ();
            aVar.ॱ();
        }

        private static boolean ˎ(Message message) {
            return message.what == 2 || message.what == 1 || message.what == 7;
        }
    }

    @NonNull
    @KeepForSdk
    protected abstract String ˋ();

    @NonNull
    @KeepForSdk
    protected abstract String ॱ();

    @KeepForSdk
    protected String ˊ() {
        return "com.google.android.gms";
    }

    @Nullable
    private final String ˋॱ() {
        return this.ʽॱ == null ? this.ˏॱ.getClass().getName() : this.ʽॱ;
    }

    @KeepForSdk
    @Nullable
    protected String ˏ() {
        return null;
    }

    @CallSuper
    @KeepForSdk
    protected void ˎ(@NonNull T t) {
        this.ᐝ = System.currentTimeMillis();
    }

    @CallSuper
    @KeepForSdk
    protected void ˋ(int i) {
        this.ॱॱ = i;
        this.ʽ = System.currentTimeMillis();
    }

    @CallSuper
    @KeepForSdk
    protected void ˋ(ConnectionResult connectionResult) {
        this.ʻ = connectionResult.ˊ();
        this.ʼ = System.currentTimeMillis();
    }

    private final void ॱ(int i, T t) {
        boolean z = true;
        if ((i == 4) != (t != null)) {
            z = false;
        }
        apa.ˊ(z);
        synchronized (this.ॱˊ) {
            this.ʻॱ = i;
            this.ॱᐝ = t;
            ˏ(i, t);
            switch (i) {
                case 1:
                    if (this.ॱˎ != null) {
                        this.ˊॱ.ˋ(this.ͺ.ˊ(), this.ͺ.ˋ(), this.ͺ.ॱ(), this.ॱˎ, ˋॱ(), this.ͺ.ˎ());
                        this.ॱˎ = null;
                        break;
                    }
                    break;
                case 2:
                case 3:
                    String ˊ;
                    String ˋ;
                    apf o_apf;
                    if (!(this.ॱˎ == null || this.ͺ == null)) {
                        ˊ = this.ͺ.ˊ();
                        ˋ = this.ͺ.ˋ();
                        Log.e("GmsClient", new StringBuilder((String.valueOf(ˊ).length() + 70) + String.valueOf(ˋ).length()).append("Calling connect() while still connected, missing disconnect() for ").append(ˊ).append(" on ").append(ˋ).toString());
                        this.ˊॱ.ˋ(this.ͺ.ˊ(), this.ͺ.ˋ(), this.ͺ.ॱ(), this.ॱˎ, ˋॱ(), this.ͺ.ˎ());
                        this.ॱ.incrementAndGet();
                    }
                    this.ॱˎ = new e(this, this.ॱ.get());
                    if (this.ʻॱ != 3 || ˏ() == null) {
                        o_apf = new apf(ˊ(), ॱ(), false, 129, ॱॱ());
                    } else {
                        o_apf = new apf(ᐝ().getPackageName(), ˏ(), true, 129, false);
                    }
                    this.ͺ = o_apf;
                    if (!this.ͺ.ˎ() || ʻ() >= 17895000) {
                        if (!this.ˊॱ.ˊ(new o.aov.c(this.ͺ.ˊ(), this.ͺ.ˋ(), this.ͺ.ॱ(), this.ͺ.ˎ()), this.ॱˎ, ˋॱ())) {
                            ˊ = this.ͺ.ˊ();
                            ˋ = this.ͺ.ˋ();
                            Log.e("GmsClient", new StringBuilder((String.valueOf(ˊ).length() + 34) + String.valueOf(ˋ).length()).append("unable to connect to service: ").append(ˊ).append(" on ").append(ˋ).toString());
                            ˎ(16, null, this.ॱ.get());
                            break;
                        }
                    }
                    ˋ = "Internal Error, the minimum apk version of this BaseGmsClient is too low to support dynamic lookup. Start service action: ";
                    String valueOf = String.valueOf(this.ͺ.ˊ());
                    if (valueOf.length() != 0) {
                        valueOf = ˋ.concat(valueOf);
                    } else {
                        valueOf = new String(ˋ);
                    }
                    throw new IllegalStateException(valueOf);
                    break;
                case 4:
                    ˎ((IInterface) t);
                    break;
            }
        }
    }

    @KeepForSdk
    void ˏ(int i, T t) {
    }

    private final boolean ˊ(int i, int i2, T t) {
        boolean z;
        synchronized (this.ॱˊ) {
            if (this.ʻॱ != i) {
                z = false;
            } else {
                ॱ(i2, (IInterface) t);
                z = true;
            }
        }
        return z;
    }

    @KeepForSdk
    public boolean ˎ() {
        boolean z;
        synchronized (this.ॱˊ) {
            z = this.ʻॱ == 4;
        }
        return z;
    }

    @KeepForSdk
    public boolean ʽ() {
        boolean z;
        synchronized (this.ॱˊ) {
            z = this.ʻॱ == 2 || this.ʻॱ == 3;
        }
        return z;
    }

    private final boolean ˊॱ() {
        boolean z;
        synchronized (this.ॱˊ) {
            z = this.ʻॱ == 3;
        }
        return z;
    }

    private final void ˏ(int i) {
        int i2;
        if (ˊॱ()) {
            i2 = 5;
            this.ʿ = true;
        } else {
            i2 = 4;
        }
        this.ˎ.sendMessage(this.ˎ.obtainMessage(i2, this.ॱ.get(), 16));
    }

    @KeepForSdk
    public final Context ᐝ() {
        return this.ˏॱ;
    }

    protected final void ˎ(int i, @Nullable Bundle bundle, int i2) {
        this.ˎ.sendMessage(this.ˎ.obtainMessage(7, i2, -1, new f(this, i, null)));
    }

    @KeepForSdk
    protected boolean ʼ() {
        return false;
    }

    private final boolean ॱˊ() {
        if (this.ʿ || TextUtils.isEmpty(ˋ()) || TextUtils.isEmpty(ˏ())) {
            return false;
        }
        try {
            Class.forName(ˋ());
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    @KeepForSdk
    public int ʻ() {
        return aoj.ˏ;
    }

    @KeepForSdk
    protected boolean ॱॱ() {
        return false;
    }
}
